<template>
	<view>
		<view class="">
			<swiper
				class="swiper-box"
				:autoplay="true"
				:interval="3000"
				:duration="1000">
				<swiper-item
					v-for="(item,i) in details.pics"
					:key="i">
					<view class="swiper-item">
						<image
							:src="item" mode="aspectFill"
							:style="{backgroundColor:item}"
							class="swiper-img" />
					</view>
				</swiper-item>
			</swiper>
			
			<view class="prize-info">
				<view class="prize-do">
					<view class="prize-num">
						<text>{{ details.score_price }}</text>
						<text>泌豆</text>
					</view>
					<!-- <view class="prize-stock">
						<text>库存999</text>
					</view> -->
				</view>
				<view class="prize-name">
					<text>{{ details.name }}</text>
				</view>
				<!-- <view class="prize-desc">
					<text>{{ '我是副标题副标题副标题副标题副标题副标题副标题副标题' }}</text>
				</view> -->
			</view>
			
			<view class="prize-explain">
				<view class="thead">
					<view class="title">
						<text>Product details</text>
					</view>
					<view class="line">
						<text>商品详情</text>
					</view>
				</view>
				<view v-html="content"></view>
			</view>
			
			<view class="subBox">
				<view class="submitBtn flex" @click="open('popup')">开始兑换</view>
			</view>
		</view>
		
		<uni-popup
			ref="popup"
			type="bottom">
			<view class="pop-box">
				<view class="popup-thead">
					<text>收货地址</text>
				</view>
				<view class="order-address" v-if="addressMsg==null">
					<view
						class="flex align"
						style="justify-content: center;"
						@click="$toPage('/package/mine/setAddress/setAddress?setNew=true')">
						<image
							src="https://wx.2024csnl.com/static/goods/order-add.png"
							mode="aspectFill"
							class="order-add" />
						<view class="add-address">添加地址</view>
					</view>
				</view>
				<view
					class="order-address address-checked"
					v-else
					@click="onSkip('/package/mine/myAddress/myAddress?pageType=true')">
					<view class="address-info">
						<view class="address-addr">{{addressMsg.area}}{{addressMsg.address}}</view>
					</view>
					<view class="info">
						<text>{{addressMsg.name}}</text>
						<text class="tel">{{addressMsg.tel}}</text>
					</view>
				</view>
				<view class="confirm-btn">
					<view class="inner">
						<button
							class="btn"
							@click="showPayKeyboard">确认兑换</button>
					</view>
				</view>
			</view>
		</uni-popup>
		
		<pay-keyboard
			:show_key="show_pay_keyboard"
			@hideFun="hidePayKeyboard"
			:show_forget="true"
			@getPassword="getPayPassword">
		</pay-keyboard>
	</view>
</template>

<script>
	import {
		setShoppingExchange,
		setShoppingDetails,
		checkpaypwdRequest
	} from "../../api/index.js"
	export default {
		data() {
			return {
				show_pay_keyboard: false,
				payPassword: '',
				addressMsg: {},
				isAddress: false,
				details: {},
				content: '',
				id: '',
			}
		},
		async onLoad(opt) {
			this.id = opt.id
			await this.onSetShoppingDetails(opt.id)
		},
		async onShow() {
			// 选取地址
			uni.$on('selectAddress', async (data) => {

				this.addressMsg = data
			})
			// 监听是否刷新
			uni.$on('refresh', async (data) => {
				// console.log('监听到刷新,',data)
				this.onSetShoppingDetails(this.id)
			})
		},
		beforeDestroy() {
			uni.$off('selectAddress');
		},
		methods: {
			async showPayKeyboard() {
				this.show_pay_keyboard = true
			},
			hidePayKeyboard() {
				this.show_pay_keyboard = false
			},
			async getPayPassword(n) {
				console.log("用户输入的密码", n)
				this.payPassword = n.password;
				this.show_pay_keyboard = false
			
				const res = await checkpaypwdRequest('s=/ApiMy/checkpaypwd', {
					paypwd: this.payPassword
				})
				const {
					id,
					status,
					msg
				} = res.data
			
				if (status == 1) {
					await this.onSubBtn()
				} else {
					this.showToast(msg)
				}
			},
			// 确认兑换
			async onSubBtn() {
				if (this.addressMsg == null) {
					this.showToast('请完善收货地址~')
				} else {
					// console.log(this.addressMsg.id, ' this.addressMsg.id')
					const res = await setShoppingExchange('s=/ApiScoreshop/scorecreate', {
						address_id: this.addressMsg.id,
						goods_id: this.id
					})
					const {
						status,
						msg
					} = res.data
					// console.log(res, 'resjdj')
					this.showToast(msg)
					if (status == 1) {
						this.close('popup')
					}
				}


			},
			async onSetShoppingDetails(id) {
				this.showAddress = false
				const res = await setShoppingDetails('s=/ApiScoreshop/scoredetail', {
					id: id
				})
				// console.log(res, "res----")
				const {
					status,
					data,
					address,
					msg
				} = res.data
				if (status == 1) {
					this.addressMsg = address
					this.details = data
					const html = JSON.parse(data.detail)
					this.content = html[0].content
					// console.log(JSON.parse(dat[0].guigedata))
				} else {
					this.showToast(msg)
				}
			},
			open(n) {
				this.$refs[n].open();
			},
			close(n) {
				this.$refs[n].close();
			},
		}
	}
</script>

<style>
	page {
		background: #F7F7F7;
	}

	.p-c {
		color: #B3B3B3;
	}
</style>

<style lang="scss" scoped>
	.swiper-box,
	.swiper-item,
	.swiper-img {
		width: 100vw;
		height: 100vw;
	}

	.subBox {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		background: #fff;
	}

	.submitBtn {
		margin: 50rpx 30rpx;
		color: #fff;
		font-size: 34rpx;
		background: -webkit-linear-gradient(left, #41644A, #263A29);
		border-radius: 90rpx;
		padding: 30rpx 0;
		justify-content: center;
	}

	.prize-img {
		width: 100%;
		height: 468rpx;
	}

	.prize-info {
		padding: 20rpx 20rpx 40rpx;
		border-bottom: 1rpx solid #F7F7F7;
		background-color: #FFF;
		font-size: 29rpx;
		.prize-do {
			position: relative;
			display: flex;
			align-items: center;
			line-height: 80rpx;
			.prize-num {
				flex: 1;
				color: #E86A33;
				font-size: 56rpx;
				font-weight: bold;
				text {
					&:last-child {
						font-size: 34rpx;
						font-weight: normal;
						color: #999999;
						margin-left: 10rpx;
					}
				}
			}
			.prize-stock {
				font-size: 30rpx;
				color: #999999;
			}
		}
		.prize-name {
			line-height: 40rpx;
			font-size: 34rpx;
			margin: 10rpx 0 20rpx;
			font-weight: bold;
		}
		.prize-desc {
			line-height: 40rpx;
			color: #999999;
			font-weight: normal;
		}
		.prize-initiator {
			font-weight: normal;
			font-size: 24rpx;
			margin-top: 16rpx;
			color: #B3B3B3;
		}
	}

	.prize-explain {
		font-size: 24rpx;
		padding: 40rpx 0;
		background-color: #FFF;
		padding-bottom: 200rpx;
		border-top: 20rpx solid #f8f8f8;
		.thead {
			margin-bottom: 40rpx;
			text-align: center;
			.title {
				line-height: 80rpx;
				font-size: 44rpx;
				font-weight: bold;
				text-transform: uppercase;
				color: #000;
			}
			.line {
				font-size: 32rpx;
				color: #999;
				display: flex;
				align-items: center;
				justify-content: center;
				&:before,
				&::after {
					content: '';
					width: 15vw;
					height: 1rpx;
					border-top: 1px solid #dddddd;
					margin: 0 30rpx;
				}
			}
		}
	}

	.order-address {
		margin: 20rpx 0 0rpx;
		padding: 15rpx 0rpx 20rpx;
		min-height: 160rpx;
		.address-info {
			font-size: 32rpx;
			font-weight: bold;
			background: url('https://img.2024csnl.com/static/2.x/a41.png') left center no-repeat;
			background-size: 40rpx;
			padding: 0 0 0 50rpx;
			.address-addr {
				padding-right: 80rpx;
				background: url('https://img.2024csnl.com/static/2.x/a42.png') right center no-repeat;
				background-size: 50rpx;
			}
		}
		.info {
			color: #999999;
			font-size: 30rpx;
			margin: 20rpx 0 20rpx 50rpx;
			text {
				color: #000000;
				margin-right: 20rpx;
				&.tel {
					color: #999;
				}
			}
		}
		.order-add {
			width: 32rpx;
			height: 32rpx;
			margin-right: 15rpx;
		}
		.add-address {
			color: #41644A;
			line-height: 160rpx;
			font-size: 28rpx;
			text-align: center;
		}
	}
	
	.address-checked {
		margin-top: 0;
		border-radius: 0 0 20rpx 20rpx;
	}
	
	.pop-box {
		background-color: #FFFFFF;
		padding: 10rpx 40rpx 40rpx;
		border-radius: 30rpx 30rpx 0 0;
		overflow-y: auto;
		.popup-thead {
			height: 100rpx;
			line-height: 100rpx;
			font-size: 34rpx;
			font-weight: bold;
		}
	}
	
	.confirm-btn {
		position: relative;
		height: 100rpx;
		.inner {
			position: fixed;
			left: 40rpx;
			right: 40rpx;
			bottom: 40rpx;
			z-index: 1;
			.btn {
				height: 100rpx;
				line-height: 100rpx;
				border-radius: 100rpx;
				background: -webkit-linear-gradient(left, #41644A, #263A29);
				color: #FFFFFF;
				font-size: 34rpx;
			}
		}
	}
</style>